package com.hxc.shop.service;

import com.hxc.shop.bean.ShopGoodsAddress;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.mapper.Mysql.AddressMapper;
import com.hxc.shop.mapper.Mysql.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AddressService {
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private UserMapper userMapper;

    public ResultVO findGoodsAddress(String userName){
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到用户名,获取购物地址失败!");
        }
        // 检查是否有该用户
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("查找用户名失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户名失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户名，获取失败!");
        }
        List<ShopGoodsAddress> shopGoodsAddressList = new ArrayList<>();
        try {
             shopGoodsAddressList = addressMapper.findGoodsAddress(userName);
        }catch (Exception e) {
            log.error("获取购物地址失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取购物地址失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsAddressList);
    }

    public ResultVO addGoodsAddress(String userName,String goodsAddress) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空,添加失败!");
        }
        if (StringUtils.isEmpty(goodsAddress)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"购物地址为空，添加失败!");
        }
        // 检查是否有该用户
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("查找用户名失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户名失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户名，添加失败!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ShopGoodsAddress shopGoodsAddress = new ShopGoodsAddress();
        shopGoodsAddress.setAddress(goodsAddress);
        shopGoodsAddress.setUserName(userName);
        shopGoodsAddress.setCreateTime(sdf.format(new Date()));
        shopGoodsAddress.setUpdateTime(sdf.format(new Date()));
        try {
            addressMapper.addGoodsAddress(shopGoodsAddress);
        }catch (Exception e) {
            log.error("添加购物地址失败:"+userName+"=="+goodsAddress+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"添加购物地址失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"添加购物地址成功!");
    }

    public ResultVO deleteGoodsAddress(Integer id) {
        if (id == 0 || id == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到购物地址编号,删除失败!");
        }
        // 检查是否有该购物地址编号
        Integer addressId = 0;
        try {
            addressId = addressMapper.findAddressIdById(id);
        }catch (Exception e) {
            log.error("查找购物地址编号失败:"+id+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找购物地址编号失败:"+e.getMessage());
        }
        if (addressId == null || addressId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此购物地址编号，删除失败!");
        }

        try {
            addressMapper.deleteGoodsAddress(id);
        }catch (Exception e) {
            log.error("删除购物地址失败:"+id+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"删除购物地址失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"删除购物地址成功!购物地址编号:"+id);
    }

    public ResultVO changeGoodsAddress(Integer id,String userName,String address){
        if (id == 0 || id == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到购物地址编号,修改购物地址失败!");
        }
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，修改购物地址失败!");
        }
        if (StringUtils.isEmpty(address)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"购物地址为空，修改购物地址失败!");
        }
        // 检查是否有该购物地址编号
        Integer addressId = 0;
        try {
            addressId = addressMapper.findAddressIdById(id);
        }catch (Exception e) {
            log.error("查找购物地址编号失败:"+id+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找购物地址编号失败:"+e.getMessage());
        }
        if (addressId == null || addressId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此购物地址编号，修改失败!");
        }
        // 检查是否有该用户
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("查找用户名失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户名失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户名，修改失败!");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ShopGoodsAddress shopGoodsAddress = new ShopGoodsAddress();
        shopGoodsAddress.setUpdateTime(sdf.format(new Date()));
        shopGoodsAddress.setUserName(userName);
        shopGoodsAddress.setAddress(address);
        shopGoodsAddress.setId(id);
        try {
            addressMapper.changeGoodsAddress(shopGoodsAddress);
        }catch (Exception e) {
            log.error("修改购物地址失败:"+id+"=="+userName+"=="+address+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"修改购物地址失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"修改购物地址成功!");
    }

    public ResultVO getGoodsAddressByIdAndUserName(Integer id,String userName) {
        if (id == 0 || id == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到购物地址编号,获取购物地址失败!");
        }
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，获取购物地址失败!");
        }
        // 检查是否有该购物地址编号
        Integer addressId = 0;
        try {
            addressId = addressMapper.findAddressIdById(id);
        }catch (Exception e) {
            log.error("查找购物地址编号失败:"+id+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找购物地址编号失败:"+e.getMessage());
        }
        if (addressId == null || addressId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此购物地址编号，获取失败!");
        }
        // 检查是否有该用户
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("查找用户名失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户名失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户名，获取失败!");
        }
        String address = "";
        try {
            address = addressMapper.getGoodsAddressByIdAndUserName(id,userName);
        }catch (Exception e) {
            log.error("获取购物地址失败:"+id+"=="+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取购物地址失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,address);
    }

}
